<?php
$sqlErrorText = '';
$sqlErrorCode = 0;
$sqlStmt = '';
$sqlFileToExecute = 'emptyDB.sql';
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>MySQL Executor</title>
    </head>
    <body>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
            <table width="100%">
                <tr>
                    <td>DbName:</td>
                    <td><input name="dbname" type="text" /></td>
                </tr>
                <tr>
                    <td>Hostname:</td>
                    <td><input name="hostname" type="text" /></td>
                </tr>
                <tr>
                    <td>Username:</td>
                    <td><input name="username" type="text" /></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input name="password" type="password" /></td>
                </tr>
                <tr>
                    <td align="center" colspan="2">
                        <input type="submit" name="submitBtn" value="Install" />
                    </td>
                </tr>
            </table>  
        </form>
        <?php
        if (isset($_POST['submitBtn'])) {
            $host = isset($_POST['hostname']) ? $_POST['hostname'] : '';
            $user = isset($_POST['username']) ? $_POST['username'] : '';
            $pass = isset($_POST['password']) ? $_POST['password'] : '';
            $dbname = isset($_POST['dbname']) ? $_POST['dbname'] : '';

            $con = mysql_connect($host, $user, $pass);
            if (!mysql_select_db($dbname)) {
                $sqlErrorCode = 1;
                $sqlErrorCode = mysql_errno();
                $sqlErrorText = mysql_error();
                mysql_close();                
            }
            if ($con !== false) {
                // Load and explode the sql file
                $f = fopen($sqlFileToExecute, "r+");
                $sqlFile = fread($f, filesize($sqlFileToExecute));
                $sqlArray = explode(';', $sqlFile);

                //Process the sql file by statements
                foreach ($sqlArray as $stmt) {
                    if (strlen($stmt) > 3) {
                        $result = mysql_query($stmt);
                        if (!$result) {
                            $sqlErrorCode = mysql_errno();
                            $sqlErrorText = mysql_error();
                            $sqlStmt = $stmt;
                            break;
                        }
                    }
                }
            }

            echo '<table width="100%">';
            if ($sqlErrorCode == 0) {
                echo "<tr><td>Installation was finished succesfully!</td></tr>";
            } else {
                echo "<tr><td>An error occured during installation!</td></tr>";
                echo "<tr><td>Error code: $sqlErrorCode</td></tr>";
                echo "<tr><td>Error text: $sqlErrorText</td></tr>";
                echo "<tr><td>Statement:<br/> $sqlStmt</td></tr>";
            }
            echo '</table>';
        }
        ?>
    </body>  
</html>